import {
  getOrders,
  addOrder
} from '../../api/orderApi.js'

import router from '../../router/index.js'

export default {
  namespaced: true,
  state: {
    //当前用户订单数据
    order_list: [],
    //订单状态
    order_status: {
      '0': '已下单',
      '2': '已付款',
      '4': '已配货',
      '6': '已发货',
      '8': '已收货',
      '10': '已评价',
      '30': '退款中',
      '32': '已退款',
      '34': '退款失败',
      '40': '已取消'
    }
  },
  getters: {},
  mutations: {
    set_order_list(state, value) {
      state.order_list = value;
    }
  },
  actions: {
    //获取订单列表
    get_order_list(context) {
      getOrders().then(function(response) {
        if (response.data.httpcode == 200) {
          context.commit('set_order_list', response.data.data);
        }
      })
    },
    //添加订单
    add_order(context) {
      // 订单信息
      let order = {
        //订单商品总金额
        order_spuamount: context.rootState.cart.total,
        //订单配送费
        order_expressfee: 0,
        // 订单总金额
        order_totalamount: context.rootState.cart.total,
        //折扣金额
        order_discountamount: 0,
        //实付金额
        order_payamount: context.rootState.cart.total
      }
      //收货信息
      let addr = {
        //收货人姓名	文本	赵六
        'orderAddress.consignee': context.rootState.address.default_address.uaddr_name,
        //收货人电话	文本	13600000002
        'orderAddress.phone': context.rootState.address.default_address.uaddr_phone,
        //收货一级行政地区	文本	上海市
        'orderAddress.province': context.rootState.address.default_address.uaddr_province,
        //收货二级行政地区	文本	黄浦区
        'orderAddress.city': context.rootState.address.default_address.uaddr_city,
        //收货三级行政地区	文本	市区
        'orderAddress.district': context.rootState.address.default_address.uaddr_district,
        //收货详细地址
        'orderAddress.address': context.rootState.address.default_address.uaddr_address
      }
      //购物车中商品
      let orderDetailList = [];
      let index = 0;
      context.rootState.cart.cart_list.forEach(function(cart) {
        //取勾选的商品
        if (cart.cart_checked == 1) {
          let orderDetail = `{
           "orderDetailList[${index}].odtails_name":"${cart.cart_name}",
           "orderDetailList[${index}].odtails_thumburl":"${cart.cart_thumburl}",
           "orderDetailList[${index}].odtails_price":"${cart.cart_price}",
           "orderDetailList[${index}].odtails_count":"${cart.cart_count}",
           "orderDetailList[${index}].odtails_amount":"${cart.cart_price * cart.cart_count}",
           "orderDetailList[${index}].odtails_scorestatus":"0",
           "orderDetailList[${index}].odtails_spu_id":"${cart.cart_spuid}",
           "orderDetailList[${index}].odtails_sku":${JSON.stringify(cart.cart_sku)}
           }`;
          orderDetailList.push(JSON.parse(orderDetail));
          index++;
        }
      })

      let order_submit = {}
      orderDetailList.forEach(function(odtails, index) { //遍历数组
        for (let key in odtails) {
          order_submit[key] = odtails[key];
        }
      })
      //提交订单
      addOrder({
        ...order,
        ...addr,
        ...order_submit
      }).then(function(response) {
        if (response.data.httpcode == 200) {
          alert(`订单创建成功! 订单号为：${response.data.data.order_no}`);
          //将购物车中已生成订单的数据删除
          const cartList = context.rootState.cart.cart_list;
          cartList.forEach(function(cart) {
            if (cart.cart_checked == 1) {
              context.dispatch('cart/delete_cart', cart, {
                root: true
              })
            }
          })
          //跳转到订单页面
          router.push({
            path: '/orderlist'
          })
        }
      })
    }
  }
}
